package com.attendance.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.attendance.common.CommonResult;
import com.attendance.pojo.Pages;
import com.attendance.vo.AttendanceVO;
import com.attendance.vo.EntranceVO;
import com.attendance.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;
    /**
     * 获取门禁打卡记录列表
     *
     * @return 封装的门禁打卡记录列表
     */
    @GetMapping("/entranceList")
    public CommonResult findEntranceList(String dateBegin, String dateEnd, EntranceVO entranceVO, Pages pages) {

        //把参数放入map
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("dateBegin", dateBegin);
        paramMap.put("dateEnd", dateEnd);
        paramMap.put("entranceVO", entranceVO);

        //查询返回并封装分页信息
        return attendanceService.findStudentEntranceByParamMap(paramMap, pages);
    }

    /**
     * 查询考勤记录表
     *
     * @return
     */
    @GetMapping("/attendanceList")
    public CommonResult findAttendanceList(String dateBegin, String dateEnd, AttendanceVO attendanceVO, Pages pages) {

        HashMap<String, Object> paramMap = new HashMap<>();

        paramMap.put("dateBegin", dateBegin);
        paramMap.put("dateEnd", dateEnd);
        paramMap.put("attendanceVO", attendanceVO);

        return attendanceService.findStudentAttendanceByParamMap(paramMap, pages);
    }

    /**
     * 门禁打卡记录导出
     *
     * @return
     */
    @GetMapping("/entranceDownload")
    public void entranceDownload(HttpServletResponse response, @RequestBody(required = false) List<EntranceVO> entrances) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("门禁打卡记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<EntranceVO> entranceVOS = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(), EntranceVO.class).sheet("模板").doWrite(entrances);
    }
    /**
     * 考勤记录导出
     *
     * @return
     */
    @GetMapping("/attendanceDownload")
    public void attendanceDownload(HttpServletResponse response, @RequestBody(required = false) List<AttendanceVO> attendances) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("考勤打卡记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), AttendanceVO.class).sheet("模板").doWrite(attendances);
    }
    /**
     * 考勤记录导入
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ResponseBody
    public CommonResult upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), AttendanceVO.class, new ReadListener<AttendanceVO>() {
            private static final int BATCH_COUNT = 10;

            private List<AttendanceVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            @Override
            public void invoke(AttendanceVO attendanceVO, AnalysisContext context) {
                //导入数据库方法
                cachedDataList.add(attendanceVO);
                if (cachedDataList.size() >= BATCH_COUNT) {
                    attendanceService.uploadAttendanceRecord(cachedDataList);
                    // 存储完成清理 list
                    cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                attendanceService.uploadAttendanceRecord(cachedDataList);
            }
        }).sheet().doRead();
        return CommonResult.success("上传成功");
    }
}